﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
	public class campaignClickDetailAIMOutput : Api_BaseOutput
	{
		public campaignClickDetailAIMResults result { get; set; }
		public campaignClickDetailAIMParms inputParms { get; private set; }
		// --------------------------------------------------------
		public campaignClickDetailAIMOutput( campaignClickDetailAIMInput input )
			: base( input )
		{
			inputParms = input.parms;
			result = new campaignClickDetailAIMResults();
		}
		// --------------------------------------------------- tostring
		public override string ToString()
		{
			return string.Format( "Campaign Id: {0}, Url: {1}, Click Detail Count: {2}",
				this.inputParms.cid, this.inputParms.url, this.result.data.Count );
		}

		#region ================================================================== format setup

		// ---- serial
		internal void Format( string api_Request, string api_Response )
		{
			this.api_Request = api_Request;
			this.api_Response = api_Response;
			// ----------------------------------
			if ( this.api_OutputType == EnumValues.OutputType.JSON )
				outputJSON();
			else if ( this.api_OutputType == EnumValues.OutputType.XML )
				outputXML();
		}

		// ---- xmlrpc
		internal void Format( x_campaignClickDetailAIM rpcResult )
		{
			this.api_Request = EnumValues.NotSupplied;
			this.api_Response = EnumValues.NotSupplied;
			// ------------------------
			this.result = FormatResults( rpcResult );
		}

		#endregion

		#region ================================================================= Output

		// ---------------------------------------------------------- outputXML
		private void outputXML()
		{
			XElement xml = XElement.Parse( this.api_Response );
			// error check
			Api_Error error = apiSerial.GetXmlOutputError( xml );
			if ( error != null )
			{
				this.api_ErrorMessages.Add( error );
				return;
			}
			// -------------------------------------------- get values
			int cnt;
			try
			{
				int.TryParse( xml.Element( "total" ).Value, out cnt );
				result.total = cnt;
				//
				IEnumerable<XElement> aim = xml.Element( "data" ).Elements( "struct" );
				foreach ( XElement entry in aim )
				{
					campaignClickDetailAIMResults.DataItem item = new campaignClickDetailAIMResults.DataItem();
					item.email = entry.Element( "email" ).Value;
					int.TryParse( entry.Element( "clicks" ).Value, out cnt );
					item.clicks = cnt;

					this.result.data.Add( item );
				}
			}
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}

		// ---------------------------------------------------------- outputJSON
		private void outputJSON()
		{
			JavaScriptSerializer jss = new JavaScriptSerializer();

			try
			{
				if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
				{
					Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
					this.api_ErrorMessages.Add( err );
				}
				else
				{
					x_campaignClickDetailAIM jsonResult = jss.Deserialize<x_campaignClickDetailAIM>( this.api_Response );
					this.result = FormatResults( jsonResult );
				}
			}
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}

		// ---------------------------------------------------------- FormatResults
		private campaignClickDetailAIMResults FormatResults( x_campaignClickDetailAIM x_results )
		{
			campaignClickDetailAIMResults results = new campaignClickDetailAIMResults();
			results.total = x_results.total;
			//
			if ( x_results.data == null || x_results.data.Length < 1 ) return results; // ignore empty set

			foreach ( x_campaignClickDetailAIM.data_item entry in x_results.data )
			{
				campaignClickDetailAIMResults.DataItem item = new campaignClickDetailAIMResults.DataItem
				{
					email = entry.email,
					clicks = entry.clicks
				};
				results.data.Add( item );
			}
			return results;
		}

		#endregion
	}
}
